//
//  DGGAliyunTokenRequest.m
//  AliyunOSSiOS
//
//  Created by 胡中楷 on 2020/7/3.
//

#import "DGGAliyunTokenRequest.h"
#import "DGGAliyunSignUtil.h"

/// 服务器地址
//static NSString *dgg_get_token_url = @"https://osstoken.dgg188.cn/v1/sts?";
//static NSString *dgg_get_token_url = @"https://tmicrouag.dgg188.cn/external/oss/sts";
static NSString *dgg_get_token_url = @"https://microuag.dgg188.cn/external/oss/sts";

@interface DGGAliyunTokenRequest()

@property (nonatomic, copy) NSString *serverURL;
@property (nonatomic, strong) DGGAliyunSignUtil *signUtil;

@end

@implementation DGGAliyunTokenRequest

- (instancetype)init{
    self = [super init];
    if(self){
        _serverURL = dgg_get_token_url;
    }
    return self;
}

- (void)requestAliyunTokenGetWithParams:(NSDictionary *)params headers:(NSDictionary *)headers completionHandler:(RequestAliyunTokenResultBlock)completion{
    NSMutableURLRequest *mutableURLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.serverURL]];
    mutableURLRequest.HTTPMethod = @"GET";
    [mutableURLRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    mutableURLRequest.timeoutInterval = 30;
    ///添加请求头
    for (NSString *key in headers.allKeys) {
        NSString *value = [params valueForKey:key];
        if(value){
            [mutableURLRequest setValue:value forHTTPHeaderField:key];
        }
    }
    ///处理参数
    if(params && params.count > 0){
        NSData *paramsData = [NSJSONSerialization dataWithJSONObject:params options:(NSJSONWritingPrettyPrinted) error:nil];
        if(paramsData){
            mutableURLRequest.HTTPBody = paramsData;
        }
    }
    NSDictionary *signParams = [self.signUtil requestSignForGet:mutableURLRequest.URL.absoluteString params:params sysKey:self.serverSysKey sysCode:self.serverSysCode];
    for (NSString *signKey in signParams.allKeys) {
        id signValue = signParams[signKey];
        if(signValue && signValue != [NSNull null]){
            [mutableURLRequest setValue:signValue forHTTPHeaderField:signKey];
        }
    }
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:mutableURLRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if(error){
            if(completion){
                completion(nil, NO, error);
            }
        }else{
            NSError *jsonError;
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
            if(jsonError){
                if(completion){
                    completion(nil, NO, error);
                }
            }else{
                if(completion){
                    completion(dict, YES, error);
                }
            }
        }
    }];
    [dataTask resume];
}

- (DGGAliyunSignUtil *)signUtil{
    if(!_signUtil){
        _signUtil = [[DGGAliyunSignUtil alloc] init];
    }
    return _signUtil;
}

@end
